using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
//using System.Windows.Controls;
using System.Windows.Forms;
using System.Linq;

using P2.SimpleAccounting.Modules.Users.DAL;
using P2.SimpleAccounting.Modules.Users.Services;

using P2.Framework.BOLBase;
using P2.Framework.Helper;
using P2.Framework.Helper.Configuration;
using System.ComponentModel;
using System.Collections.ObjectModel;
using System.Collections.Specialized;



namespace P2.SimpleAccounting.Modules.Users.BusinessEntities
{
    public class UsersItem : BizObject, INotifyPropertyChanged
    {
        public override int ObjectID
        {
            get
            {
                return 9998;
            }
        }
        #region "Constructors"
        // add new Instance
        public UsersItem()
        {

            Modified = false;
        }
        public UsersItem(DAL.UsersItem UsersItem)
        {

            Load(UsersItem);

        }

        #endregion

        //open an exsiting Instance



        #region Members

        int _UsersItemID = -1;
        public int UsersItemID
        {
            get { return _UsersItemID; }
            protected set
            {
                if (value != _UsersItemID)
                {
                    _UsersItemID = value;
                    Modified = true;
                }
            }
        }


        

        int _UsersHeaderID = -1;
        public int UsersHeaderID
        {
            get { return _UsersHeaderID; }
            set
            {
                if (value != _UsersHeaderID)
                {
                    _UsersHeaderID = value;
                    Modified = true;
                }
            }
        }


        int? _FiAccountID = -1;
        public int? FiAccountID
        {
            get { return _FiAccountID; }
            set
            {
                if (value != _FiAccountID)
                {
                    _FiAccountID = value;
                    Modified = true;
                }
            }
        }


        decimal _UsersItemQuantity = 0;
        public decimal UsersItemQuantity
        {
            get { return _UsersItemQuantity; }
            set
            {
                if (value != UsersItemQuantity)
                {
                    _UsersItemQuantity = value;
                    Modified = true;
                }
            }
        }

        decimal _UsersItemAmount = 0;
        public decimal UsersItemAmount
        {
            get { return _UsersItemAmount; }
            set
            {
                if (value != _UsersItemAmount)
                {
                    _UsersItemAmount = value;
                    Modified = true;
                }
            }
        }

              
        public Users ParentUsers { set; get; }

        
        #endregion

        #region "Read operations"

        //load an existing Instance
        public void Load(DAL.UsersItem UsersItem)
        {

            if (UsersItem != null)
            {
                this.UsersItemID = UsersItem.UsersItemID;
                this.UsersHeaderID = UsersItem.UsersHeaderID.GetValueOrDefault(-1);
                                
                this.UsersItemQuantity = UsersItem.UsersItemQuantity.GetValueOrDefault(0);
                this.UsersItemAmount = UsersItem.UsersItemAmount.GetValueOrDefault(0);
                
                this.StatusID = P2.Framework.Helper.Workflows.WorkflowStatus.GetInitialStatus(-1).StatusID;
                this.Timestamp = UsersItem.TStamp;
            }
            else
            {
                this.UsersItemID = -1;
                this.UsersHeaderID = -1;
                
                this.FiAccountID = null;
                this.UsersItemQuantity = 0;
                this.UsersItemAmount = 0;
                
                P2.Framework.Helper.Workflows.WorkflowStatus initStatus = P2.Framework.Helper.Workflows.WorkflowStatus.GetInitialStatus(-1);
                this.StatusID = initStatus.StatusID;

            }
            Modified = false;
        }

        protected P2TransactionMessage FillMyObject()
        {
            P2TransactionMessage tMsg = new P2TransactionMessage();

            try
            {
                string connectionString = ConfigurationSetUp.GetConnectionString("Business");
                using (UsersDataModelDataContext db = new UsersDataModelDataContext(connectionString))
                {
                    var Userss = from UsersHeaderItem in db.UsersItems
                                                  where UsersHeaderItem.UsersItemID == this._UsersItemID
                                                  select UsersHeaderItem;
                    if (Userss.Count() > 0)
                    {
                        foreach (var Users in Userss)
                        {
                            Load(Users);
                        }
                    }
                }



                tMsg.TransactionStatus = "green";
                tMsg.MessageText = "TMsg_FillMyObjectSuceeded";
            }
            catch (Exception ex)
            {
                tMsg.TransactionStatus = "red";
                tMsg.MessageText = "Error_Error";
                tMsg.MessageFreeText = ex.Message;
                P2ExceptionHandler.LogException(ex, this);
            }
            //}

            this.MessageStack.Add(tMsg);
            return tMsg;
        }

        public static ObservableCollection<UsersItem> GetUsersItemsForHeader(Users Users, PropertyChangedEventHandler onChange)
        {

            ObservableCollection<UsersItem> itemsCollection = new ObservableCollection<UsersItem>();

            string connectionString = ConfigurationSetUp.GetConnectionString("Business");
            using (UsersDataModelDataContext db = new UsersDataModelDataContext(connectionString))
            {

                var UsersItems = from UsersItem in db.UsersItems
                                                  where UsersItem.UsersHeaderID == Users.UsersHeaderID
                                                  select UsersItem;
                        if (UsersItems.Count() > 0)
                        {
                            foreach (var UsersItem in UsersItems)
                            {
                                itemsCollection.Add(new UsersItem(UsersItem));
                            }
                        }

                                  
            }

            return itemsCollection;
        }
        #endregion
        
        #region "Update operations"
        public P2MessageStack InsertAndUpdate(UsersDataModelDataContext db)
        {
            P2TransactionMessage tMsg = new P2TransactionMessage();
            //tMsg = base.InsertAndUpdate(checkDoublicate);

            tMsg.TransactionStatus = "green";
            P2MessageStack MessageStack = new P2MessageStack();


            if (tMsg.TransactionStatus == "green")
            {
                try
                {
                    P2.Framework.Security.AccessPermissions ap = new P2.Framework.Security.AccessPermissions();
                    if (string.IsNullOrEmpty(this.CreatedBy)) this.CreatedBy = ap.UserID;
                    this.EditedBy = ap.UserID;

                    if (this.UsersItemID < 0)
                    {
                        int? retUsersItemID = -1;
                        db.fiInsertUsersItem(this.UsersHeaderID, this.FiAccountID, this.UsersItemQuantity, this.UsersItemAmount,
                             this.CreatedBy, ref retUsersItemID);
                        if (retUsersItemID.HasValue)
                        {
                            this.UsersItemID = retUsersItemID.Value;

                            MessageStack.Add(new P2TransactionMessage("green", Resources.TMsg_UsersItemAdded));
                        }
                        else
                        {
                            MessageStack.Add(new P2TransactionMessage("red", Resources.TMsg_UsersNameEntUpdated));
                        }
                    }
                    else
                    {
                        int? rowsAffected = -1;
                        db.fiUpdateUsersItem(this.UsersHeaderID, this.FiAccountID, this.UsersItemQuantity, this.UsersItemAmount, 
                            this.UsersItemID, this.EditedBy, this.Timestamp, ref rowsAffected);

                        if (rowsAffected == 0)
                        {
                            MessageStack.Add(new P2TransactionMessage("red", Resources.TMsg_UsersNameEntUpdated));
                        }
                        else
                        {

                            MessageStack.Add(new P2TransactionMessage("green", Resources.TMsg_UsersItemUpdated));
                        }
                    }
                    if (MessageStack.StatusMessage.MessageStatus != P2ValidationStatus.red)
                    {
                        var Users = (from UsersHeaderItem in db.UsersItems
                                                      where UsersHeaderItem.UsersItemID == this._UsersItemID
                                                      select UsersHeaderItem).Single();
                        Load(Users);

                    }

                }
                catch (Exception ex)
                {
                    MessageStack.Add(new P2TransactionMessage("red", P2.Framework.Helper.Localize.Localize.Error_Error));
                    P2ExceptionHandler.LogException(ex, this);
                }


            }
            return MessageStack;
        }

        public void Delete(UsersDataModelDataContext db)
        {

            int? rowsAffected2 = -1;
            db.fiDeleteUsersItem(this.UsersItemID, this.Timestamp, ref rowsAffected2);

        }
        #endregion

        #region Validation


        #endregion

        #region "Copy operations"
        public UsersItem GetCopy()
        {
            UsersItem newItem = new UsersItem();
            newItem.UsersHeaderID = this.UsersHeaderID;
            newItem.UsersItemID = this.UsersItemID;
            
            
            newItem.UsersItemAmount = this.UsersItemAmount;
            newItem.UsersItemQuantity = this.UsersItemQuantity;            
            
            newItem.ParentUsers = this.ParentUsers;
            newItem.Timestamp = this.Timestamp;
            newItem.Modified = this.Modified;
            return newItem;
        }
        public void UpdateFrom(UsersItem sourceItem)
        {

            this.UsersHeaderID = sourceItem.UsersHeaderID;
            this.UsersItemID = sourceItem.UsersItemID;
                        
            this.UsersItemAmount = sourceItem.UsersItemAmount;
            this.UsersItemQuantity = sourceItem.UsersItemQuantity;
            
            
            this.ParentUsers = sourceItem.ParentUsers;
            this.Timestamp = sourceItem.Timestamp;
            this.Modified = sourceItem.Modified;

        }
        #endregion






        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        #endregion
        private void OnPropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler Handler = PropertyChanged;
            if (Handler != null) Handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public void OnAllPropertiesChanged()
        {
            OnPropertyChanged("UsersItemID");            
            OnPropertyChanged("UsersHeaderID");
                        
            OnPropertyChanged("UsersItemQuantity");
            OnPropertyChanged("UsersItemAmount");            

        }
    }
}
